<?php
require(dirname(dirname(__FILE__)). DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'unit.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'test', true);
new sfDatabaseManager($configuration);
$loader = new sfPropelData();

$testData = array(
	1	=>	array(
				'topic'			=> 'Title is the title',
				'text'			=> 'This is a text body.',
				'deleted'		=> false,
				'user_id'		=> 1,
				'course_id'		=> 1,
				'semester_id'		=> 8,
				'type'			=> 'lecture',
				'number'		=> 1,
				'lecturer_name'		=> 'John Doe',
				'lecturer_email'	=> 'nobody@home.now',
				'summary'		=> 'This is the summary.',
				'visibility'		=> 1
			),
	2	=>	array(
				'topic'			=> 'This is the second title',
				'text'			=> 'This is a different text body.',
				'deleted'		=> false
			),
	3	=>	array(
				'topic'			=> 'This is the third title',
				'text'			=> 'This is yet another text body.',
				'deleted'		=> false
			),
	'nonversion'	=>	array(
				'topic'			=> 'Title for non-version',
				'deleted'		=> false
			)
);
$who = 1;
$t = new lime_test();


/**
  * This should create VERSION 1 and return a non-false result. Additionally, getId() should return a non-zero integer.
  */
$note = new Note();
$note->fromArray($testData[1],BasePeer::TYPE_FIELDNAME);
$note->setVersionCreatedBy($who);
$note->setVersionComment('a');
$result = $note->save(null,1);
$t->ok($note->getVersion() == 1);
$t->ok($result!==false);
$note_id = $note->getId();
$t->ok($note_id>0);


$t->diag("Working with Note ID #{$note_id}");

/**
 * Terminal Version test #1.
 */
$t->ok($note->isTerminalVersion());


/**
 * This should create VERSION 2
 */
$note = NotePeer::retrieveByPk($note_id);
$t->ok($note->getVersion()==1);
$note->fromArray($testData[2],BasePeer::TYPE_FIELDNAME);
$note->setVersionCreatedBy($who);
$note->setVersionComment('b');
$result = $note->save();
$t->ok($result!==false);
$version2 = $note->getVersion();
$t->ok($note->getVersion()==2);

/**
 * Terminal Version test #2.
 * Two versions now exist without delete flags. Terminal version should return false.
 */
$t->ok(!$note->isTerminalVersion());


/**
 * Update the live version without touching the versioning system (on VERSION 2, versioning off)
 * This illustrates how to edit data on the live version without creating a new revision.
 */
$note = NotePeer::retrieveByPk($note_id);
$note->versioning(false);
$t->ok($note->versioning()===false);
$note->fromArray($testData['nonversion'],BasePeer::TYPE_FIELDNAME);
$result = $note->save();
$t->ok($result!==false);
$t->ok($note->getVersion()==2);


/**
 * Re-enable versioning and save VERSION 3, with a comment
 * Turn versioning back on and make sure version creation still works.
 */
$note = NotePeer::retrieveByPk($note_id);
$note->versioning(true);
$note->fromArray($testData[3],BasePeer::TYPE_FIELDNAME);
$note->setVersionCreatedBy($who);
$note->setVersionComment('c');
$result == $note->save();
$t->ok($result!==false);
$n = NotePeer::retrieveByPk($note_id);
$t->ok($n->getVersionCreatedBy()==$who);
$t->ok($n->getVersionComment()=='c');


/**
 * COUNT VERSIONS
 * Make sure we are still at 3 versions, not 4 (versioning turned off for save 1 revision ago)
 */
$note = NotePeer::retrieveByPk($note_id);
$data = $note->getAllVersions();
$t->ok(count($data)==3);

/**
 * VERSION CONGRUENCY
 * Make sure version numbers are still contiguous
 */
$note = NotePeer::retrieveByPk($note_id);
$note->setId($note_id);
$prev_version = $note->getPreviousVersionNumber();
$t->ok($prev_version==2);

/**
 * SOFT DELETE of version that is not live
 * Marks the revision as deleted and performs no other action.
 */
$note = NotePeer::retrieveByPk($note_id);
$note->retrieveByVersion(2);
$data = $note->toArray();
$t->ok(!$data['Deleted']);
$note = NotePeer::retrieveByPk($note_id);
$t->ok($note->versionDelete(2));
$note = NotePeer::retrieveByPk($note_id);
$note->toVersion(2);
$data = $note->toArray();
$t->ok($data['Deleted']);

/**
 * SOFT DELETE of version that is live
 * Marks the version as deleted and takes the most recent non-deleted revision, creating a new one out of it, and marks it live.
 */
$note = NotePeer::retrieveByPk($note_id);
$note->retrieveByVersion(3);
$data = $note->toArray();
$t->ok(!$data['Deleted']);
$note = NotePeer::retrieveByPk($note_id);
$t->ok($note->versionDelete(3,1,'This is my comment'));
$note = NotePeer::retrieveByPk($note_id);
$note->toVersion(3);
$data = $note->toArray();
$t->ok($data['Deleted']);
$note = NotePeer::retrieveByPk($note_id);
$note->toVersion(4);
$data = $note->toArray();
$t->ok($data['Topic'] == $testData[1]['topic']);
 



